<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <el-container :style="{'min-height': containerHeight + 'px'}">
            <el-container style="border-top: 1px solid rgb(88, 149, 194)">
                <transition name="el-zoom-in-top">
                    <el-aside
                        :width="asideWidth"
                        style="background-color: rgb(238, 241, 246)"
                        v-show="condition"
                    >
                        <el-tree
                            ref="elTree"
                            node-key="id"
                            :data="treeData"
                            :props="defaultProps"
                            @node-click="handleNodeClick"
                            @current-change="currentChange"
                            default-expand-all
                            :highlight-current="true"
                            :expand-on-click-node="false"
                        ></el-tree>
                    </el-aside>
                </transition>
                <!-- 收起栏 -->
                <div class="tree_show">
                    <div @click="treeShow" class="icon_click">
                        <i :class="icon_direction"></i>
                    </div>
                </div>
                <el-main style="background-color: rgb(238, 241, 246)">
                    <el-header style="text-align: left; font-size: 12px">
                        <el-button type="primary" icon="el-icon-folder-add" @click="goAdd">新增</el-button>
                        <el-button type="warning" icon="el-icon-edit" @click="goEdit">编辑</el-button>
                        <el-button type="danger" icon="el-icon-delete" @click="delAllSelection">删除</el-button>
                    </el-header>
                    <table-List
                        ref="tableList"
                        :tableData="tableData"
                        :tableHeader="tableHeader"
                        :loading="loading"
                        :pagination="pagination"
                        :pageShow="false"
                        @selectFun="selectFun"
                    ></table-List>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>



<script>
import appMain from '../../../utils/app_main';
export default {
    data() {
        return {
            containerHeight: '',
            pageTitle: '',
            icon_direction: 'el-icon-arrow-left',
            condition: true,
            asideWidth: '300px',
            treeData: [],
            defaultProps: {
                children: 'children',
                label: 'name',
                value: 'id'
            },
            // 通用表格
            loading: false,
            pagination: {
                pageIndex: 1,
                pageSize: 10,
                pageTotal: 0
            },
            tableData: [
                // {
                //     id: '', //
                //     hierarchy: '', //层级：1岗位，2中层，3高层
                //     isKernel: '', //是否是核心岗位，0不是  1是
                //     jobNumber: '', //岗位编号
                //     name: '', //岗位名称
                //     organizationId: '', //所属部门，关联organization表
                //     description: '', //岗位描述
                //     responsibility: '' //岗位职责
                // }
            ],
            tableHeader: [
                // 表头数据
                { prop: 'organizationName', label: '所属部门' },
                {
                    prop: 'jobNumber',
                    label: '岗位编号'
                },
                {
                    prop: 'name',
                    label: '岗位名称'
                },
                {
                    prop: 'hierarchy',
                    label: '岗位类别',
                    formatData: function(val) {
                        if (val === 1) {
                            return '岗位';
                        } else if (val === 2) {
                            return '中层';
                        } else if (val === 3) {
                            return '高层';
                        }
                    }
                },
                { prop: 'responsibility', label: '岗位职责' },
                { prop: 'description', label: '岗位描述', minWidth: '100px' }
            ],
            multipleSelection: [],
            treeId: {}
        };
    },
    created() {
        this.getTree();
    },
    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 160;
        this.pageTitle = this.$router.currentRoute.meta.title;
    },
    methods: {
        treeShow() {
            this.condition = !this.condition;
            if (this.condition) {
                this.asideWidth = '300px';
                this.icon_direction = 'el-icon-arrow-left';
            } else {
                this.asideWidth = '0px';
                this.icon_direction = 'el-icon-arrow-right';
            }
        },
        // 获取树的数据
        getTree() {
            appMain
                .organizationList()
                .then(res => {
                    if (res.data.code === 20000) {
                        this.treeData = res.data.data;
                    } else {
                        this.$message.error('服务器繁忙，请稍后再试');
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // 树元素点击事件
        handleNodeClick(e) {
            console.log(e, 'treeItem');
            this.treeId = e;
            this.tableData = _.cloneDeep(e.jobs);
            for (const key in this.tableData) {
                this.tableData[key].organizationName = e.name;
            }
        },
        currentChange(node, obj) {
            console.log('', this.tableData);
        },
        goAdd() {
            if (this.treeId != {}) {
                this.$router.push({
                    path: '/jobsetting',
                    query: {
                        treeId: this.treeId
                    }
                });
            } else {
                this.$router.push({
                    path: '/jobsetting'
                });
            }
        },
        goEdit() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }

            this.$router.push({
                path: '/jobsetting',
                query: {
                    id: this.multipleSelection[0].id
                }
            });
        },
        delAllSelection() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //请求接口删除数据
                    this.deleteById(this.multipleSelection[0].id);
                })
                .catch(() => {});
        },
        deleteById(id) {
            appMain
                .jobInfoDel({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.getTree();
                        //拿到当前所有节点
                        this.tableData = [
                            // {
                            //     id: '', //
                            //     hierarchy: '', //层级：1岗位，2中层，3高层
                            //     isKernel: '', //是否是核心岗位，0不是  1是
                            //     jobNumber: '', //岗位编号
                            //     name: '', //岗位名称
                            //     organizationId: '', //所属部门，关联organization表
                            //     description: '', //岗位描述
                            //     responsibility: '' //岗位职责
                            // }
                        ];
                        console.log('eltree');
                        //this.$refs.elTree;
                        //console.log('eltree', this.$refs.elTree);
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },

        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        //表格
        // 分页导航
        pageChange(data) {},
        // 组件选择完后把数据传过来
        selectFun(data) {
            this.multipleSelection = data.backData;
            console.log(this.multipleSelection);
        },
        //表格组件返回排序对象
        sortChange(data) {}
    }
};
</script>

<style lang="css" scoped>
.el-header {
    line-height: 60px;
}
.icon_click {
    padding-top: 23px;
    height: 50px;
    width: 100%;
    background-color: rgb(169, 203, 229);
    text-align: center;
}

.tree_show {
    width: 16px;
    background-color: rgb(88, 149, 194);
    padding-top: 20rem;
}
.el-aside {
    color: #333;
}

.el-select,
.el-input,
el-radio-group {
    width: 360px !important;
}
</style>